package org.javaboy.pub_sub_demo.consumer;

import org.javaboy.pub_sub_demo.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 第一种消费方式，实现 接口 MessageListener 接口
 */
@Component
public class MyConsumer01 implements MessageListener {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //消息内容
//        byte[] body = message.getBody();
//        String s = new String(body);
        Book book = (Book) redisTemplate.getValueSerializer().deserialize(message.getBody());
        System.out.println("MyConsumer01 收到消息：" + book);
    }
}
